<?php
require_once './buildCurl.php';
error_reporting(E_ALL || ~E_NOTICE);
header('content-type:application/json;charset=utf-8');
$res = json_decode(file_get_contents('php://input'), true);

$term0 = $res['term'][0];
$term1 = $res['term'][1];

$session = $res['session'];

$now_year = date('Y');

switch ($term0) {
    case 0:
        $term0 = ($now_year - 3) . '-' . ($now_year - 2);
        break;
    case 1:
        $term0 = ($now_year - 2) . '-' . ($now_year - 1);
        break;
    case 2:
        $term0 = ($now_year - 1) . '-' . ($now_year - 0);
        break;
    case 3:
        $term0 = ($now_year - 0) . '-' . ($now_year + 1);
        break;
}

switch ($term1) {
    case 0:
        $term1 = '-1-1';
        break;
    case 1:
        $term1 = '-2-1';
        break;
}


$class = $res['class'];


$url = 'http://211.82.48.36/student/teachingResources/classCurriculum/searchCurriculumInfo/callback?planCode=' . $term0 . $term1 . '&classCode=' . $class;

$myCurl = new MyCurl($url, $session);
$result = $myCurl->getResult();
$httpCode = $myCurl->getHttpCode();

if ($httpCode != 200) {
    $arr = array('err' => 1);
    exit(json_encode($arr));
}

$content = json_decode($result, true);

$day1 = [];
$day2 = [];
$day3 = [];
$day4 = [];
$day5 = [];
$day6 = [];
$day7 = [];

$list0 = $content[0];
foreach ($list0 as $value) {
    $teacher = $value['jsm'];
    $name = $value['kcm'];
    $start_c = $value['id']['skjc'];
    $length = $value['cxjc'];
    $weeks = $value['zcsm'];
    $weeks = explode('周', $weeks)[0];
    $calssDay = $value['id']['skxq'];
    if (strpos($weeks, '-') > 0) {
        $start_w = (int)explode('-', $weeks)[0];
        $end_w = (int)explode('-', $weeks)[1];
    } else {
        $start_w = (int)explode('第', $weeks)[1];
        $end_w = $start_w;
    }
    //$where = $value['xqm'].' '.$value['jxlm'].$value['jasm'];
    $where = $value['jxlm'] . $value['jasm'];
    $arr = array('name' => $name, 'teacher' => $teacher, 'start_c' => $start_c, 'length' => $length, 'start_w' => $start_w, 'end_w' => $end_w, 'where' => $where);

    switch ($calssDay) {
        case 1:
            $day1[] = $arr;
            break;
        case 2:
            $day2[] = $arr;
            break;
        case 3:
            $day3[] = $arr;
            break;
        case 4:
            $day4[] = $arr;
            break;
        case 5:
            $day5[] = $arr;
            break;
        case 6:
            $day6[] = $arr;
            break;
        case 7:
            $day7[] = $arr;
            break;
    }
}


$temp_kb = array($day1, $day2, $day3, $day4, $day5, $day6, $day7);
$arr = array('err' => 0, 'kebiao_data' => $temp_kb, 'url' => $url);
$res = json_encode($arr);
$new_res =  str_replace('\\', '\\\\', $res);

exit($res);
